﻿//
// Created by work on 24-6-6.
//

#include "iot_model_device/CRtModelYxInfo.h"

namespace iotplat {
    std::map<CRtModelYxInfo::EModelYxCol, std::string> CRtModelYxInfo::rtModelYxColToStringMap = {
            {CRtModelYxInfo::EValues, "valuesmap"}
    };

    std::map<CRtModelYxInfo::EModelYxCol, CValue::EValueType> CRtModelYxInfo::rtYxDataTypeByField = {
            {CRtModelYxInfo::EValues, CValue::IOT_String}
    };

    Json::Value CRtModelYxInfo::toJson() {
        Json::Value json;
        json[baseSigFieldName(BASE_SIG_ENUM::EModel_name)] = model_name;
        json[baseSigFieldName(BASE_SIG_ENUM::EUser)] = user;
        json[baseSigFieldName(BASE_SIG_ENUM::EName)] = name;
        json[baseSigFieldName(BASE_SIG_ENUM::EDescr)] = descr;
        json[baseSigFieldName(BASE_SIG_ENUM::ESn)] = sn;
        json[baseSigFieldName(BASE_SIG_ENUM::EGrouping)] = grouping;
        json[baseSigFieldName(BASE_SIG_ENUM::EConfigs)] = configs;
        json[yxFieldName(YX_ENUM::EValues)] = this->valuesToJson();
        return json;
    }

    Json::Value CRtModelYxInfo::valuesToJson()const {
        Json::Value arr(Json::ValueType::arrayValue);
        for(const auto& it : values) {
            arr.append(it.toJson());
        }
        return arr;
    }

    void CRtModelYxInfo::valuesFromJson(const Json::Value &json) {
        values.clear();
        if(!json.isArray())
            return;

        for(const auto& obj : json) {
            SYxValue value;
            value.fromJson(obj);
            values.push_back(value);
        }
    }

    void CRtModelYxInfo::fromJson(Json::Value json) {
        model_name = json[baseSigFieldName(BASE_SIG_ENUM::EModel_name)].asString();
        user = json[baseSigFieldName(BASE_SIG_ENUM::EUser)].asString();
        name = json[baseSigFieldName(BASE_SIG_ENUM::EName)].asString();
        descr = json[baseSigFieldName(BASE_SIG_ENUM::EDescr)].asString();
        sn = json[baseSigFieldName(BASE_SIG_ENUM::ESn)].asInt();
        grouping = json[baseSigFieldName(BASE_SIG_ENUM::EGrouping)].asString();
        configs = json[baseSigFieldName(BASE_SIG_ENUM::EConfigs)];
        valuesFromJson(json[yxFieldName(YX_ENUM::EValues)]);
    }

    CRtModelYxInfo::CRtModelYxInfo() {

    }
} // iotplatl
